#include "CountSensor.h"
#include "OLED.h"

uint16_t CountSensor_Count=0;

void CountSensor_Init(void){
	// RCC时钟配置
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB,ENABLE);
	
	// GPIOB配置
	GPIO_InitTypeDef GPIO_InitStruct={
		GPIO_Pin_14,
		GPIO_Speed_50MHz,
		GPIO_Mode_IPU
	};
	GPIO_Init(GPIOB,&GPIO_InitStruct);
	
	// AFIO选择GPIO管脚用作外部中断线路
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);
	
	// EXIT配置
	EXTI_InitTypeDef EXTI_InitStruct={
		EXTI_Line14,
		EXTI_Mode_Interrupt,
		EXTI_Trigger_Rising,
		ENABLE,
	};
	EXTI_Init(&EXTI_InitStruct);
	
	// NVIC配置
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStruct={
		EXTI15_10_IRQn,
		1,
		1,
		ENABLE,
	};
	NVIC_Init(&NVIC_InitStruct);
}

uint16_t CountSensor_Get(void){
	return CountSensor_Count;
}

void EXTI15_10_IRQHandler(void){
	if(EXTI_GetITStatus(EXTI_Line14)==SET){
		CountSensor_Count++;
		EXTI_ClearITPendingBit(EXTI_Line14);
	}
}
